<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
    margin: 0;
    padding: 0;
}
.box{
    overflow: none;
}
.box span{
    float: left;
    font-size: 20px;
    color: lightskyblue;
}
.box a,.box p{
    float: left;
    font-size: 20px;
    color: lightcoral;
    margin-left: 100px;
}
</style>
</head>
<body>
    
<h1>项目-首页</h1>

<div class="box">
    <span>请登录</span>
    <a href="./5-login.html">登录</a>
    <a href="./5-shopping.html">购物车</a>
    <p>退出登录</p>
</div>

<script src="./utils.js"></script>
<script>
// 存储当前页面地址
localStorage.setItem('currentUrl',location.href)

// 获取本地存储中的token
let token = localStorage.getItem('token')

// 发送验证是否登录的请求
let xhr = new XMLHttpRequest()
xhr.open('get','http://localhost:8080/users/islogin')

// 在请求头中携带token发送到服务端
xhr.setRequestHeader('authorization',token)

xhr.send(null)

xhr.onload = function (){
    let json = JSON.parse(xhr.responseText)// 解析成JSON对象
    if (json.code == 1) {
        // 已登录，显示用户账户
        $('.box span').innerText = json.data.user
    }
}


$('.box p').onclick = function (){
    // 退出登录
    localStorage.removeItem('token')
    // 刷新页面
    location.reload()
}

</script>
</body>
</html>